﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace SkyPoint
{
    public class StarLocator
    {
        FixedSkyCatalogue catalogue;
        PlanetUtils planetUtils;

        public StarLocator()
        {
            planetUtils = new PlanetUtils();
            catalogue = new FixedSkyCatalogue();
        }

        public List<SkyObject> GetNearbyObjects(Coord coordinate)
        {
            List<SkyObject> ret = new List<SkyObject>();
            ret.AddRange(getNearybyStars(coordinate));
            ret.AddRange(getNearbyPlanets(coordinate));


            return ret;
        }

        private IEnumerable<SkyObject> getNearbyPlanets(Coord coordinate)
        {
            List<SkyObject> ret = new List<SkyObject>();
            for (int iter = 0; iter < 8; iter++)
            {
                Coord planetLocation = planetUtils.getPlanetCoordinates(iter, DateTime.UtcNow);
                if(Utils.ArcDistance(coordinate,planetLocation) < Math.PI/6 && iter != 3)
                {
                    ret.Add(new SkyObject(planetLocation, planetUtils.PlanetNames[iter],"",planetUtils.PlanetMagnitudes[iter],new Color()));
                }
            }

            return ret;
        }

        private IEnumerable<SkyObject> getNearybyStars(Coord coordinate)
        {
            List<SkyObject> ret = catalogue.NearbyStars(coordinate);
            if (ret.Count >= 4)
            {
                return ret.GetRange(0, 4);
            }
            else
                return ret;
        }
    }
}
